from django.db import models
from django.contrib.auth.models import AbstractUser, UserManager
from util.typing import DjangoModelTyping
from django.contrib.auth.models import Group
from redis import Redis
from django.conf import settings


class UserManage(UserManager):

    def create_superuser(self, username, email, password, **extra_fields):
        user = super(UserManage, self).create_superuser(
            username,
            email,
            password,
            **extra_fields
        )

        '''
        自定义用户
        '''

        return user

    def create_user(self, username, email=None, password=None, **extra_fields):
        extra_fields['is_superuser'] = False
        user = super(UserManage, self).create_user(
            username,
            email,
            password,
            **extra_fields
        )

        '''
        自定义用户
        '''

        return user


class User(AbstractUser):
    phone = models.CharField(
        max_length=25,
        verbose_name='用户手机号',
        null=False,
        default=''
    )
    avatar = models.CharField(
        max_length=255,
        verbose_name='用户手机号',
        null=False,
        default='http://121.36.172.117/avatar.jpeg'
    )
    objects = UserManage()
    parent_user = models.ForeignKey(
        'user.User',
        on_delete=models.SET_NULL,
        null=True,
        verbose_name='上级用户'
    )

    def online_keys(self):
        red: Redis = Redis.from_url(settings.SOCKET_SID_SAVE_REDIS_URL)
        return red.keys(f"{self.username}*")

    class Meta(DjangoModelTyping):
        db_table = 'users'
        permissions = (
            ('user_get', '获取用户'),
            ('user_patch', '修改用户'),
            ('user_delete', '删除用户'),
            ('user_post', '新建用户')
        )
        default_permissions = ()


class WebSiteSetting(models.Model):

    logo = models.CharField(
        max_length=255,
        verbose_name='网站logo'
    )
    name = models.CharField(
        max_length=255,
        verbose_name='网站名称',
        
    )
    primary_color = models.CharField(
        max_length=255,
        verbose_name='主题颜色',
        null=True
    )
    info_color = models.CharField(
        max_length=255,
        null=True,
        verbose_name='提示颜色'
    )
    success_color = models.CharField(
        max_length=255,
        null=True,
        verbose_name='成功颜色'
    )
    error_color = models.CharField(
        max_length=255,
        null=True,
        verbose_name='错误颜色'
    )
    layout_header_background = models.CharField(
        max_length=255,
        null=True,
        verbose_name='侧边栏颜色'
    )
    user = models.OneToOneField(
        User,
        on_delete=models.CASCADE,
        
    )
    class Meta:
        db_table = "website_setting"
        default_permissions = ()


class UnderGroups(models.Model):
    """
    用于区分用户创建的Group权限组
    """
    user = models.ForeignKey(
        User,
        on_delete=models.SET_NULL,
        null=True
    )
    group = models.ForeignKey(
        Group,
        on_delete=models.SET_NULL,
        null=True
    )

    class Meta(DjangoModelTyping):
        db_table = "under_groups"
        unique_together = ('user', 'group')
        default_permissions = ()
